package com.jintian.smart.kernel.flowable.component.action.process;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

import com.jintian.smart.kernel.core.auth.IOperator;
import com.jintian.smart.kernel.core.flow.FlowAction;
import com.jintian.smart.kernel.core.flow.ITaskOperation;
import com.jintian.smart.kernel.core.flow.ProcessControl;
import com.jintian.smart.kernel.flowable.component.DefaultTaskOperation;
import com.jintian.smart.kernel.flowable.component.action.base.AbstractProcessAction;
import com.jintian.smart.kernel.flowable.context.DefaultProcessContext;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ResumeProcessAction extends AbstractProcessAction<List<TaskInfo>> {
    @Autowired
    private ITaskOperation<List<TaskInfo>> taskOperation;

    @Override
    public FlowAction getAction() {
        return FlowAction.resume;
    }

    @Override
    protected List<TaskInfo> innerExecute(TaskEntity task, ProcessControl processControl) throws Exception {
        ProcessInstance pi = this.getProcessInstance(task.getProcessInstanceId());
        if (!pi.isSuspended()) {
            throw new IllegalArgumentException("流程已恢复");
        }
        try (DefaultProcessContext context = (DefaultProcessContext) this.processContextProvider.createInstance(
                pi.getProcessDefinitionKey(), task.getTaskDefinitionKey(), this.getAction(),
                task.getProcessInstanceId(), null, pi.getBusinessKey(), processControl);) {
            this.beforeAction(task, context, this.getAction());
            List<TaskInfo> ret = this.invokeAction(context, new Callable<List<TaskInfo>>() {
                @Override
                public List<TaskInfo> call() throws Exception {
                    runtimeService().activateProcessInstanceById(task.getProcessInstanceId());
                    return Arrays.asList(loadTask(task.getId()));
                }
            });
            // 启动后立即流转
            if (Boolean.TRUE.equals(context.getVariablesLocal().get(FlowAction.VARNAME_COMPLETE_AFTER_RESUME))
                    || Boolean.TRUE.equals(context.getTransientVariablesLocal().get(FlowAction.VARNAME_COMPLETE_AFTER_RESUME))
                    || Boolean.TRUE.equals(context.getTransientVariables().get(FlowAction.VARNAME_COMPLETE_AFTER_RESUME))) {
//                processControl = this.queryProcessControl(this.getAction(), task.getId(), null);
                processControl = ((DefaultTaskOperation) taskOperation).queryProcessControl(task.getId(), FlowAction.complete.name(), context.getTransientVariables());
                return taskOperation.complete(task.getId(), processControl);
            }
            return ret;
        }
    }

    @Override
    protected String validTask(TaskEntity task, IOperator operator, TaskCheckEnum check) {
        if (check == TaskCheckEnum.Active) {
            if (task.isCanceled()) {
                return "任务已取消";
            } else {
                return null;
            }
        } else {
            return super.validTask(task, operator, check);
        }
    }
}
